package com.kinon.ogt.view.activities;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ProgressBar;

import com.kinon.ogt.R;
import com.kinon.ogt.view.widget.FxToolbar;
import com.kinon.ogt.view.widget.X5WebView;
import com.tencent.smtt.sdk.WebChromeClient;
import com.tencent.smtt.sdk.WebView;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 佛祖保佑 永无BUG
 *
 * @author WangZhiYao
 * @date 2018/4/27
 */
public class ShareActivity extends AppCompatActivity {

    private static final String TAG = "ShareActivity";

    @BindView(R.id.webView)
    X5WebView mWebView;
    @BindView(R.id.toolbar)
    FxToolbar mToolBar;
    @BindView(R.id.swipe_refresh)
    SwipeRefreshLayout mSwipeRefresh;
    @BindView(R.id.progressBar)
    ProgressBar mProgressBar;

    private String mToken;
    private String mShareText;
    private ClipboardManager mClipboard;
    private ClipboardManager.OnPrimaryClipChangedListener mClipboardChangeListener;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_share);
        ButterKnife.bind(this);

        mToolBar.setTitle("分享");
        mToolBar.setOnBackClickListener(view -> onBackPressed());
        setSupportActionBar(mToolBar);

        Intent intent = getIntent();
        if (null != intent) {
            mToken = intent.getStringExtra("TOKEN");
            initView();
            setClipboardListener();
        }
    }

    private void initView() {
        String inviteUrl = "https://ogtchain.com/game/page.html#/share/invite?token=";

        mWebView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView webView, int i) {
                if (i == 100) {
                    mProgressBar.setVisibility(View.GONE);
                    if (mSwipeRefresh.isRefreshing()) {
                        mSwipeRefresh.setRefreshing(false);
                    }
                    return;
                }

                Log.w(TAG, "refresh : " + i);

                mProgressBar.setVisibility(View.VISIBLE);
                mProgressBar.setProgress(i);

                super.onProgressChanged(webView, i);
            }
        });

        mSwipeRefresh.setOnRefreshListener(() -> {
            mWebView.clearCache(true);
            mWebView.loadUrl(inviteUrl + mToken);
        });

        mWebView.loadUrl(inviteUrl + mToken);
    }

    private void setClipboardListener() {
        final String[] tempText = {""};
        mShareText = "";
        mClipboardChangeListener = () -> {
            ClipData clipData = mClipboard.getPrimaryClip();
            ClipData.Item item = clipData.getItemAt(0);
            tempText[0] = item.getText().toString();
            if (!tempText[0].equals(mShareText)) {
                mShareText = tempText[0];
                Intent textIntent = new Intent(Intent.ACTION_SEND);
                textIntent.setType("text/plain");
                textIntent.putExtra(Intent.EXTRA_TEXT, mShareText);
                startActivity(Intent.createChooser(textIntent, "分享"));
            }
        };

        mClipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
        if (null != mClipboard) {
            mClipboard.addPrimaryClipChangedListener(mClipboardChangeListener);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mClipboard.removePrimaryClipChangedListener(mClipboardChangeListener);
    }
}
